<?php
/**
 * Talents come from diligence, and knowledge is gained by accumulation.
 *
 * @author: 晋<657306123@qq.com>
 * @date: 2019/8/4 23:33
 */

namespace Xin\Filesystem;

/**
 * 工厂类
 * @method static Filesystem aliyun(array $config)
 * @method static Filesystem qcloud(array $config)
 * @method static Filesystem qiniu(array $config)
 */
class Factory{

	/**
	 * 静态调用处理
	 *
	 * @param string $name
	 * @param array  $arguments
	 * @return mixed
	 * @throws \Xin\Filesystem\FilesystemAdapterNotExistException
	 */
	public static function __callStatic($name, $arguments){
		return self::make($name, $arguments[0]);
	}

	/**
	 * 构建抓取实例
	 *
	 * @param string $driver
	 * @param array  $options
	 * @return \Xin\Filesystem\FilesystemInterface
	 * @throws \Xin\Filesystem\FilesystemAdapterNotExistException
	 */
	public static function make($driver, array $options = []){
		if(stripos($driver, '\\') !== 0){
			$driver = ucwords(str_replace(['-', '_'], ' ', $driver));
			$driver = str_replace(' ', '', $driver);
			$driver = "\\Xin\\Filesystem\\Adapter\\{$driver}\\{$driver}";
		}

		if(!class_exists($driver)){
			throw new FilesystemAdapterNotExistException("文件存储驱动不存在：{$driver}");
		}

		/** @var \Xin\Filesystem\AdapterInterface $adapter */
		$adapter = new $driver($options);

		return new Filesystem($adapter);
	}

}
